package org.zjh.model;

import org.zjh.controller.GameController;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

/**
 * @description: 扑克牌类
 * @author 郑佳豪
 * @date 2024/10/4 1:06
 * @version 1.0
 */
public class Poker extends JLabel  implements MouseListener {
    //扑克牌种类
    private String name;
    //扑克牌数字大小
    private int number;
    //扑克牌是否面朝上
    private boolean up;
    //扑克牌能否被点击
    private boolean canBeClick = false;
    //扑克牌是否被点击
    //被点击 弹起状态 true
    //再次被点击 降落状态 false
    private boolean clicked = false;
    //是否是叫分牌
    private  boolean callPointPoker;
    //标识符id （代表属于某某玩家的牌）
    private String ownerId;




    public Poker() {
    }

    public Poker(String name,boolean up) {
        this.name = name;
        this.up = up;
        if (this.up){
            this.turnFront();
        }else {
            this.turnRear();
        }
        this.setSize(105, 155);
        this.setVisible(true);
        this.addMouseListener(this);
    }


    /**
     * 显示正面
     */
    public void turnFront(){
        this.setIcon(new ImageIcon("images/" +  name +  ".png"));
    }

    /**
     * 显示反面
     */
    public void turnRear(){
        this.setIcon(new ImageIcon("images/poker_bg.jpg"));
    }






    @Override
    public void mouseClicked(MouseEvent e) {}


    /**
     * 实现移动
     * @param e the event to be processed
     */
    @Override
    public void mousePressed(MouseEvent e) {
        //首先判断扑克牌是否能被点击
        if (canBeClick){
            //能被点击获取 扑克牌的坐标
            Point location = this.getLocation();
            //定义一个扑克牌移动的距离
            int step;
            //判断此时的扑克牌状态 clicked 如果是 true 就是弹起的状态 那么我们执行 下降移动 相反 执行向移动
            if (clicked){
                step = 30;
            }else {
                step = -30;
            }
            //判断完之后需要将 clicked状态 取反
            clicked = !clicked;
            //进行扑克牌的移动
            this.setBounds(location.x,location.y + step,this.getWidth(),this.getHeight());

            ArrayList<Poker> discardPokerList = GameController.getInstance().getPlayer().getDiscardPokerList();
            if(discardPokerList.contains(this)){
                discardPokerList.remove(this);
            }else{
                discardPokerList.add(this);
            }
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

    @Override
    public String toString() {
        return name ;
    }



    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public boolean isUp() {
        return up;
    }

    public void setUp(boolean up) {
        this.up = up;
    }
    public boolean isCanBeClick() {
        return canBeClick;
    }

    public void setCanBeClick(boolean canBeClick) {
        this.canBeClick = canBeClick;
    }

    public boolean isClicked() {
        return clicked;
    }

    public void setClicked(boolean clicked) {
        this.clicked = clicked;
    }

    public boolean isCallPointPoker() {
        return callPointPoker;
    }

    public void setCallPointPoker(boolean callPointPoker) {
        this.callPointPoker = callPointPoker;
    }

    public String getOwnerId() {
        return ownerId;
    }

    public void setOwnerId(String ownerId) {
        this.ownerId = ownerId;
    }
}
